package com.cms.admin.action.common;

import com.cms.admin.action.BaseAction;
import com.cms.admin.model.PageBean;
import com.cms.admin.model.UserCardType;
import com.cms.admin.model.UserCardTypeExt;
import com.cms.admin.model.UserList;
import com.cms.admin.service.common.TemplateService;
import com.cms.admin.service.usercard.UserCardService;
import com.cms.admin.util.Constants;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import net.sf.json.JSONObject;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by YYY-Work on 2017/5/12.
 */
public class UserCardTypeAction extends BaseAction implements ModelDriven<UserCardTypeExt> {

    private UserCardService userCardService;
    private TemplateService templateService;
    private UserCardTypeExt search = new UserCardTypeExt();
    private PageBean pageBean;

    private UserCardType userCardType;
    private Long cardtypeID;

    public String requestUserCardTypeList() {
        initBathPath();
        return "success";
    }

    public String requestAddUserCardType()
    {
        initBathPath();
        return "success";
    }

    public String requestEditUserCardType()
    {
        initBathPath();
        userCardType=userCardService.selectUserCardTypeByCardId(cardtypeID);
        if (null!=userCardType)
        {
            return "success";
        }

        return null;
    }


    public String ajaxQueryUserCardTypeList() {
        LOG.info("Start ArticleAction process,method is ajaxQuery");
        LOG.debug("Action is:" + search.getAction());

        pageBean = new PageBean(search.getCurrentPage(), search.getPageSize());
        List<UserCardType> userCardTypeList = userCardService.selectUserCardType(pageBean, search);

        String res = null;
        try {
            res = this.getQueryResult(userCardTypeList, pageBean);

            JSONObject o = new JSONObject();

            o.put("pageCount", pageBean.getTotalPage());
            o.put("content", res);

            return ajaxJsonSuccessMessage(o);
        } catch (Exception e) {
            LOG.error("ajaxQueryStuff", e);
            return ajaxJsonErrorMessage(Constants.INFO_SYS_ERROR);
        }
    }


    public String ajaxAddUserCardType()
    {
        initBathPath();
        search.setCreateDate(new Date());
        search.setCreateUserid(((UserList) ActionContext.getContext().getSession().get(Constants.USER_OBJECT)).getUserid());
        userCardService.addUserCardType(search);
        return "success";
    }

    public String ajaxUpdateUserCardType()
    {
        initBathPath();
        userCardService.updateUserCardType(search);
        return "success";
    }

    private String getQueryResult(List<UserCardType> userCardTypeList, PageBean page)
            throws Exception {
        initBathPath();
        HashMap map = new HashMap();
        map.put("userCardTypeList", userCardTypeList);
        map.put("page", page);
        map.put("basePath", getBasePath());

        String result = "";
        result = templateService.generateContentByFreeMarker(
                Constants.TEMPLATE_CARDTYPE_QUERY_RESULT, map);
        return result;
    }

    @Override
    public UserCardTypeExt getModel() {
        return search;
    }

    public Long getCardtypeID() {
        return cardtypeID;
    }

    public void setCardtypeID(Long cardtypeID) {
        this.cardtypeID = cardtypeID;
    }

    public UserCardType getUserCardType() {
        return userCardType;
    }

    public void setUserCardType(UserCardType userCardType) {
        this.userCardType = userCardType;
    }

    public UserCardService getUserCardService() {
        return userCardService;
    }

    public void setUserCardService(UserCardService userCardService) {
        this.userCardService = userCardService;
    }

    public TemplateService getTemplateService() {
        return templateService;
    }

    public void setTemplateService(TemplateService templateService) {
        this.templateService = templateService;
    }
}
